package com.handpay.zztong.hp.qualification.model;

import android.content.DialogInterface;
import android.text.TextUtils;

import com.handpay.framework.LuaTableUtil;
import com.handpay.framework.SecureManager;
import com.handpay.framework.fastjson.FastJsonHelper;
import com.handpay.zztong.hp.base.activity.ZZTongActivity;
import com.handpay.zztong.hp.base.callback.BusinessEngineListener;
import com.handpay.zztong.hp.base.model.base.BaseModel;
import com.handpay.zztong.hp.config.HttpUrlConstant;

import java.io.UnsupportedEncodingException;
import java.util.Hashtable;

import se.krka.kahlua.vm.LuaTable;

/**
 * @author sxshi on 2017/4/6.
 *         email:emotiona_xiaoshi@126.com
 *         describe:Describe the function  of the current class
 */

public class IDCardStatusModel extends BaseModel {
    private BusinessEngineListener<IDCardStatus> businessEngineListener;

    public boolean doSupplyData(ZZTongActivity activity, Hashtable<String, String> params, BusinessEngineListener<IDCardStatus> listener) {
        this.businessEngineListener = listener;
        return doPost(activity, HttpUrlConstant.Do_tGetSupplyAuditInfo, params, true, true);
    }

    @Override
    public boolean netResponse(String action, byte[] data) {
        if (super.netResponse(action, data, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        })) {
            return true;
        } else if (action.equals(HttpUrlConstant.Do_tGetSupplyAuditInfo)) {
            IDCardStatus idCardStatus = new IDCardStatus();
            if (data_type == DATA_TYPE.DATA_TYPE_JSON) {
                try {
                    idCardStatus = FastJsonHelper.getObject(new String(data, "UTF-8"), IDCardStatus.class);
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
            } else {
                LuaTable luaTable = LuaTableUtil.stringToLuaTable(data, true);
                if (luaTable != null) {
                    String curr_status = String.valueOf(luaTable.rawget("status").toString());
                    idCardStatus.setStatus(curr_status);
                    if (luaTable.rawget("auditInfo") != null) {
                        String auditInfo = String.valueOf(luaTable.rawget("auditInfo"));
                        auditInfo = SecureManager.getInstance().des(auditInfo, 0, null);
                        idCardStatus.setAuditInfo(auditInfo);
                    }
                    String name = String.valueOf(luaTable.rawget("name"));
                    name = SecureManager.getInstance().des(name, 0, null);
                    idCardStatus.setName(name);
                    String certNo = String.valueOf(luaTable.rawget("certNo"));
                    certNo = SecureManager.getInstance().des(certNo, 0, null);
                    idCardStatus.setCertNo(certNo);
                    String frontCCPic = String.valueOf(luaTable.rawget("frontCCPic"));
                    if (!TextUtils.isEmpty(frontCCPic)) {
                        idCardStatus.setFrontCCPic(frontCCPic);
                    }
                    String frontCCPicStatus = String.valueOf(luaTable.rawget("frontCCPicStatus"));
                    if (!TextUtils.isEmpty(frontCCPicStatus)) {
                        idCardStatus.setFrontCCPicStatus(Integer.parseInt(frontCCPicStatus));
                    } else {//当风控没返回数据  认为是审核(还有一种情况 贷补件也没有数据  此时客户端默认-1为贷补件控制照片转态)
                        idCardStatus.setFrontCCPicStatus(curr_status.equals("01") ? -1 : 0);
                    }
                    String backCCPic = String.valueOf(luaTable.rawget("backCCPic"));
                    if (!TextUtils.isEmpty(backCCPic)) {
                        idCardStatus.setBackCCPic(backCCPic);
                    }
                    String backCCPicStatus = String.valueOf(luaTable.rawget("backCCPicStatus"));
                    if (!TextUtils.isEmpty(backCCPicStatus)) {
                        idCardStatus.setBackCCPicStatus(Integer.parseInt(backCCPicStatus));
                    } else {
                        idCardStatus.setBackCCPicStatus(curr_status.equals("01") ? -1 : 0);
                    }
                    String holdPic = String.valueOf(luaTable.rawget("holdPic"));
                    if (!TextUtils.isEmpty(holdPic)) {
                        idCardStatus.setHoldPic(holdPic);
                    }
                    String holdPicStatus = String.valueOf(luaTable.rawget("holdPicStatus"));
                    if (!TextUtils.isEmpty(holdPicStatus)) {
                        idCardStatus.setHoldPicStatus(Integer.parseInt(holdPicStatus));
                    } else {
                        idCardStatus.setHoldPicStatus(curr_status.equals("01") ? -1 : 0);
                    }
                }
            }
            if (businessEngineListener != null) businessEngineListener.callbackBusinessResponse(idCardStatus);
        }
        return true;
    }
}
